package com.example.mylibrarybase.yumengmeng21;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.example.mylibrarybase.R;
import com.example.mylibrarybase.base.baseui.BaseActivity;
import com.example.mylibrarybase.utils.LogUtils;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class MainActivity_21_handler extends BaseActivity {

    private TextView mTv_handler;
    private TextView tv_start_handler;


    Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 11:

                break;
            }
            Bundle data = msg.getData();
            String string = data.getString("2","0");
            mTv_handler.setText(string);
        }
    };
    private ExecutorService mExecutorService;

    My my = new My();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    @Override
    public int getlayouXML() {
        return R.layout.activity_main_21_handler;
    }

    @Override
    public void initView() {
        mExecutorService = Executors.newFixedThreadPool(5);
        mTv_handler = (TextView) findViewById(R.id.tv_handler);
        tv_start_handler = (TextView) findViewById(R.id.tv_start_handler);
        tv_start_handler.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initData();
            }
        });

    }
    int i =0;
    boolean tag ;
    @Override
    public void initData() {
        if (!tag)
        mExecutorService.execute(my);

    }
        class My implements Runnable{

            @Override
            public void run() {
                tag =true;
                i++;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                String name = Thread.currentThread().getName();
                LogUtils.i("当前线程"+name+"执行内容是"+i);
                Message message = mHandler.obtainMessage();
                Bundle bundle = new Bundle();
                bundle.putString("2",""+i);
                message.setData(bundle);
                mHandler.sendMessage(message);
                if (i>=20){
                    //mExecutorService.shutdown();
                    tag =false;
                    i =0;
                    Message message1 = mHandler.obtainMessage();
                    Bundle bundle1 = new Bundle();
                    bundle.putString("2","0");
                    message.setData(bundle1);
                    mHandler.sendMessage(message1);
                    return;
                }else{
                    mExecutorService.execute(my);
                }
            }
        }

}
